package testiranjeSoftvera.phage.phageTest;

import static org.junit.Assert.*;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;

import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.testing.FestSwingTestCaseTemplate;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import testiranjeSoftvera.phage.PhageCore;

public class PhageCoreTest extends FestSwingTestCaseTemplate {

	FrameFixture frameFixture;

	@Before
	public void setUp() throws Exception {
		frameFixture = new FrameFixture(PhageCore.getInstance());
		frameFixture.show();
	}

	@After
	public void tearDown() throws Exception {
		frameFixture.cleanUp();
	}

	@Test
	public void test() {
		frameFixture.button("Start").click();
		frameFixture.dialog().button("Exit game").click();

		frameFixture.menuItem("About").click();

		frameFixture.optionPane().focus().requireTitle("O nama");
		frameFixture.optionPane().button().click();

		frameFixture.menuItem("Start Game").click();
		frameFixture.dialog().button().click();

		frameFixture.menuItem("Virus Editor").click();
		frameFixture.dialog().close();

	}

	@Test
	public void verifyTestNumberCorrect() {
		int i = 3;
		
		// Run test 10 times
		while (i > 0) {
			i--;
			
			Random r = new Random();

			frameFixture.button("Virus Editor").click();
			
			// Choose random virus
			frameFixture.dialog().comboBox().selectItem(r.nextInt(3) + 1);
			
			// Erase text from field and enter a new random value
			frameFixture.dialog().textBox("speed").deleteText();
			frameFixture.dialog().textBox("speed")
					.enterText(Integer.toString(r.nextInt(9) + 1));
			frameFixture.dialog().textBox("strength").deleteText();
			frameFixture.dialog().textBox("strength")
					.enterText(Integer.toString(r.nextInt(9) + 1));
			frameFixture.dialog().textBox("agility").deleteText();
			frameFixture.dialog().textBox("agility")
					.enterText(Integer.toString(r.nextInt(9) + 1));
			
			// Click done and check if ok
			frameFixture.dialog().button("Done").click();

			boolean checkIfVerified = PhageCore.getInstance().getAccManager()
					.getApplyAction().isVerified();
			assertTrue(checkIfVerified);

		}
	}

	@Test
	public void verifyZeroTestWrong() {
		int i = 3;
		while (i-- > 0) {
			
			Random r = new Random();
			frameFixture.button("Virus Editor").click();
			
			
			frameFixture.dialog().comboBox().selectItem(r.nextInt(3) + 1);
			frameFixture.dialog().textBox("speed").deleteText();
			frameFixture.dialog().textBox("speed")
					.enterText(Integer.toString(0));
			frameFixture.dialog().textBox("strength").deleteText();
			frameFixture.dialog().textBox("strength")
					.enterText(Integer.toString(0));
			frameFixture.dialog().textBox("agility").deleteText();
			frameFixture.dialog().textBox("agility")
					.enterText(Integer.toString(0));
			
			frameFixture.dialog().button("Done").click();

			boolean checkIfVerified = PhageCore.getInstance().getAccManager()
					.getApplyAction().isVerified();
			assertFalse(checkIfVerified);
		}
	}

	@Test
	public void verifyTestNumberWrong() {

		int z = 3;
		while (z > 0) {
			z--;
			Random r = new Random();

			frameFixture.button("Virus Editor").click();
			frameFixture.dialog().comboBox().selectItem(r.nextInt(3) + 1);
			frameFixture.dialog().textBox("speed").deleteText();
			frameFixture
					.dialog()
					.textBox("speed")
					.enterText(
							Integer.toString(r.nextInt(Integer.MAX_VALUE - 11) + 10));
			frameFixture.dialog().textBox("strength").deleteText();
			frameFixture
					.dialog()
					.textBox("strength")
					.enterText(
							Integer.toString(r.nextInt(Integer.MAX_VALUE - 11) + 10));
			frameFixture.dialog().textBox("agility").deleteText();

			frameFixture
					.dialog()
					.textBox("agility")
					.enterText(
							Integer.toString(r.nextInt(Integer.MAX_VALUE - 11) + 10));
			frameFixture.dialog().button("Done").click();

			boolean checkIfVerified = PhageCore.getInstance().getAccManager()
					.getApplyAction().isVerified();
			assertFalse(checkIfVerified);

		}
	}

	@Test
	public void verifyTestRandomString() {

		int g = 3;
		while (g > 0) {
			g--;
			SecureRandom random = new SecureRandom();

			frameFixture.button("Virus Editor").click();
			frameFixture.dialog().comboBox().selectItem(random.nextInt(3) + 1);
			frameFixture.dialog().textBox("speed").deleteText();
			frameFixture.dialog().textBox("speed")
					.enterText(new BigInteger(40, random).toString(32));
			frameFixture.dialog().textBox("strength").deleteText();
			frameFixture.dialog().textBox("strength")
					.enterText(new BigInteger(40, random).toString(32));
			frameFixture.dialog().textBox("agility").deleteText();

			frameFixture.dialog().textBox("agility")
					.enterText(new BigInteger(40, random).toString(32));
			frameFixture.dialog().button("Done").click();

			boolean checkIfVerified = PhageCore.getInstance().getAccManager()
					.getApplyAction().isVerified();
			assertFalse(checkIfVerified);
		}

	}

}
